package com.homer.android.sdk.db.greenrobot.daos;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;



import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.internal.DaoConfig;

public class ApDBDao extends AbstractDao<ApDB, Long>
{
    
    public static final String TABLENAME = "AP_DB";
    
    /**
     * Properties of entity ApDB.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties
    {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        
        public final static Property Bssid = new Property(1, String.class, "bssid", false, "BSSID");
        
        public final static Property Ssid = new Property(2, String.class, "ssid", false, "SSID");
        
        public final static Property Password = new Property(3, String.class, "password", false, "PASSWORD");
        
        public final static Property IsLastSelected = new Property(4, boolean.class, "isLastSelected", false,
            "IS_LAST_SELECTED");
        
        public final static Property ConfiguredFailedCount = new Property(5, int.class, "configuredFailedCount", false,
            "CONFIGURED_FAILED_COUNT");
        
        public final static Property DeviceBssids = new Property(6, String.class, "deviceBssids", false,
            "DEVICE_BSSIDS");
    };
    
    private DaoSession daoSession;
    
    public ApDBDao(DaoConfig config)
    {
        super(config);
    }
    
    public ApDBDao(DaoConfig config, DaoSession daoSession)
    {
        super(config, daoSession);
        this.daoSession = daoSession;
    }
    
    /** Creates the underlying database table. */
    public static void createTable(SQLiteDatabase db, boolean ifNotExists)
    {
        String constraint = ifNotExists ? "IF NOT EXISTS " : "";
        db.execSQL("CREATE TABLE " + constraint + "'AP_DB' (" + //
            "'_id' INTEGER PRIMARY KEY ," + // 0: id
            "'BSSID' TEXT NOT NULL ," + // 1: bssid
            "'SSID' TEXT NOT NULL ," + // 2: ssid
            "'PASSWORD' TEXT NOT NULL ," + // 3: password
            "'IS_LAST_SELECTED' INTEGER NOT NULL ," + // 4: isLastSelected
            "'CONFIGURED_FAILED_COUNT' INTEGER NOT NULL ," + // 5: configuredFailedCount
            "'DEVICE_BSSIDS' TEXT NOT NULL );"); // 6: deviceBssids
    }
    
    /** Drops the underlying database table. */
    public static void dropTable(SQLiteDatabase db, boolean ifExists)
    {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "'AP_DB'";
        db.execSQL(sql);
    }
    
    /** @inheritdoc */
    @Override
    protected void bindValues(SQLiteStatement stmt, ApDB entity)
    {
        stmt.clearBindings();
        
        Long id = entity.getId();
        if (id != null)
        {
            stmt.bindLong(1, id);
        }
        stmt.bindString(2, entity.getBssid());
        stmt.bindString(3, entity.getSsid());
        stmt.bindString(4, entity.getPassword());
        stmt.bindLong(5, entity.getIsLastSelected() ? 1l : 0l);
        stmt.bindLong(6, entity.getConfiguredFailedCount());
        stmt.bindString(7, entity.getDeviceBssids());
    }
    
    @Override
    protected void attachEntity(ApDB entity)
    {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }
    
    /** @inheritdoc */
    @Override
    public Long readKey(Cursor cursor, int offset)
    {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }
    
    /** @inheritdoc */
    @Override
    public ApDB readEntity(Cursor cursor, int offset)
    {
        ApDB entity = new ApDB( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.getString(offset + 1), // bssid
            cursor.getString(offset + 2), // ssid
            cursor.getString(offset + 3), // password
            cursor.getShort(offset + 4) != 0, // isLastSelected
            cursor.getInt(offset + 5), // configuredFailedCount
            cursor.getString(offset + 6) // deviceBssids
            );
        return entity;
    }
    
    /** @inheritdoc */
    @Override
    public void readEntity(Cursor cursor, ApDB entity, int offset)
    {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setBssid(cursor.getString(offset + 1));
        entity.setSsid(cursor.getString(offset + 2));
        entity.setPassword(cursor.getString(offset + 3));
        entity.setIsLastSelected(cursor.getShort(offset + 4) != 0);
        entity.setConfiguredFailedCount(cursor.getInt(offset + 5));
        entity.setDeviceBssids(cursor.getString(offset + 6));
    }
    
    /** @inheritdoc */
    @Override
    protected Long updateKeyAfterInsert(ApDB entity, long rowId)
    {
        entity.setId(rowId);
        return rowId;
    }
    
    /** @inheritdoc */
    @Override
    public Long getKey(ApDB entity)
    {
        if (entity != null)
        {
            return entity.getId();
        }
        else
        {
            return null;
        }
    }
    
    /** @inheritdoc */
    @Override
    protected boolean isEntityUpdateable()
    {
        return true;
    }
    
}

